package how2quit.apps.kleague.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

public class DragView extends View {
	
	private static final int DRAG_SCALE = 0;
	
	private Bitmap bitmap;
	private Paint paint;
	private int registrationX;
	private int registrationY;
	private float scale;
	private float animationScale = 0.9f;
	
	private WindowManager.LayoutParams layoutParams;
	private WindowManager windowManager;
	
	public DragView(Context context, Bitmap bitmap, int registrationX, int registrationY, int left, int top, int width, int height){
		super(context);
		
		windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		
		Matrix matrix = new Matrix();
		float scaleFactor = width;
		scaleFactor = scale = (scaleFactor + DRAG_SCALE ) / scaleFactor;
		matrix.setScale(scaleFactor, scaleFactor);
		this.bitmap = Bitmap.createBitmap(bitmap, left, top, width, height, matrix, true);
		
		this.registrationX = registrationX + (DRAG_SCALE /2 );
		this.registrationY = registrationY + (DRAG_SCALE /2 );
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(bitmap.getWidth(), bitmap.getHeight());
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if(true){
			Paint p = new Paint();
			p.setStyle(Paint.Style.FILL);
			p.setColor(0x8800dd11);
			p.setAlpha(80);
			canvas.drawRect(0, 0, getWidth(), getHeight(), p);
		}
		float scale = animationScale;
		if(scale<0.999f){
			float height = bitmap.getHeight();
			float width = bitmap.getWidth();
			float offset_w = (width-(width*scale))/2;
			float offset_h = (height-(height*scale))/2;
			
			canvas.translate(offset_w, offset_h);
			canvas.scale(scale, scale);
		}
		
		Paint p2 = new Paint();
		p2.setAlpha(100);
		canvas.drawBitmap(bitmap, 0.0f, 0.0f, p2);
	}

	@Override
	protected void onDetachedFromWindow() {
		super.onDetachedFromWindow();
		bitmap.recycle();
	}
	
	public void setScale(float scale){
		if(scale > 1.0f) animationScale = 1.0f;
		else animationScale = scale;
		invalidate();
	}
	
	public void show(IBinder windowToken, int touchX, int touchY){
		WindowManager.LayoutParams layoutParam;
		int pixelFormat;
		
		pixelFormat = PixelFormat.TRANSLUCENT;
		
		layoutParam = new WindowManager.LayoutParams(
					ViewGroup.LayoutParams.WRAP_CONTENT,
					ViewGroup.LayoutParams.WRAP_CONTENT,
					touchX - registrationX, touchY - registrationY,
					WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL,
					WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
					pixelFormat);
		layoutParam.gravity = Gravity.LEFT | Gravity.TOP;
		layoutParam.token = windowToken;
		layoutParam.setTitle("DragView");
		
		this.layoutParams = layoutParam;
		windowManager.addView(this, layoutParam);
	}
	
	void move(int touchX, int touchY){
		WindowManager.LayoutParams lp = this.layoutParams;
		lp.x = touchX - registrationX;
		lp.y = touchY - registrationY;
		
		windowManager.updateViewLayout(this, lp);
	}

	void remove(){
		windowManager.removeView(this);
	}
}
